package com.example.shop_2304.xq

import android.R.attr
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2304.R
import com.example.shop_2304.adapter.MyTabEntity
import com.example.shop_2304.adapter.VpAdapter
import com.example.shop_2304.bannerfg.BannerFragment
import com.example.shop_2304.bannerfg.VVFragment
import com.example.shop_2304.databinding.ActivityXqBinding
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage


@Route(path = "/app/XqActivity")
class XqActivity : BaseActivity<ActivityXqBinding,XqViewModel>() {
    val  list = mutableListOf<Fragment>()
    lateinit var vpAdapter: VpAdapter
    val tabs = ArrayList<CustomTabEntity>()
    val fglist = mutableListOf<Fragment>()
    lateinit var vpAdapter2: VpAdapter
    @Autowired
    @JvmField
    var gid = 1
    @SuppressLint("MissingInflatedId")
    override fun initView() {
        ARouter.getInstance().inject(this)
        //上方vp的轮播
        list.add(VVFragment())
        list.add(BannerFragment())
        vpAdapter = VpAdapter(this,list)
        binding.vpBanner.adapter = vpAdapter
        //详情和评论tab页面
        tabs.add(MyTabEntity("详情",0,0))
        tabs.add(MyTabEntity("评论",0,0))
        binding.common.setTabData(tabs)

        fglist.add(XqFragment())
        fglist.add(CommentFragment())
        vpAdapter2 = VpAdapter(this,fglist)
        binding.vp.adapter = vpAdapter2
        //上下联动
        binding.common.setOnTabSelectListener(object:OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                binding.vp.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        binding.vp.registerOnPageChangeCallback(object:ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.common.currentTab = position
            }
        })

        binding.tvAddCar.setOnClickListener {
            viewModel.addCar(1,gid.toString(),gid)//注意 3和7 会报错
        }
        //点击返回图标，结束当前界面
        binding.ivBack.setOnClickListener {
            finish()
        }

        //点击图片，生成二维码
        binding.ivShare.setOnClickListener {
           //弹出对话框，显示二维码图标，确定分享，调用友盟
            val view = LayoutInflater.from(this).inflate(R.layout.item_share, null)
            val bitmap =
                BarcodeEncoder().encodeBitmap("分享商品id为$gid", BarcodeFormat.QR_CODE, 200, 200)
            view.findViewById<ImageView>(R.id.iv).setImageBitmap(bitmap)
            AlertDialog.Builder(this)
                .setTitle("提示")
                .setView(view)
                .setNegativeButton("取消"){a,b->}
                .setPositiveButton("分享"){a,b->
                    //调用分享框，实现分享
                    val image: UMImage = UMImage(this, bitmap) //bitmap文件
                    ShareAction(this).withText("hello")
                        .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                        .withMedia(image)
                        .setCallback(object:UMShareListener{
                            override fun onStart(p0: SHARE_MEDIA?) {
                            }

                            override fun onResult(p0: SHARE_MEDIA?) {
                            }

                            override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                                //报错
                                ToastUtils.showLong(p1?.message)
                            }

                            override fun onCancel(p0: SHARE_MEDIA?) {
                            }
                        }).open();
                }
                .show()
        }

        //点击购物车图标，跳转到购物页面
        binding.tvGocar.setOnClickListener {
            ARouter.getInstance().build("/app/MainActivity").withInt("index",2).navigation()
        }

    }

    override fun initData() {
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)

        }
    }

    override fun getViewModel(): Class<XqViewModel>  = XqViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_xq

}